package com.lwj.excel;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddressList;

import java.util.HashMap;
import java.util.Map;

@ContentRowHeight(15)
@ColumnWidth(30)
public class TestExcel {
    @ExcelProperty(value = "一级指标名称")
    private String name ;
    @ExcelProperty(value = "二级指标名称")
    private String empId ;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmpId() {
        return empId;
    }

    public void setEmpId(String empId) {
        this.empId = empId;
    }

    public static SheetWriteHandler writeHandler(){
         return new SheetWriteHandler() {
             @Override
             public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
                 //定义一个map key是需要添加下拉框的列的index value是下拉框数据
                 Map<Integer, String[]> mapDropDown = new HashMap<>(3);
                 //设置可选值
                 String[] nameSelect = {"一级指标名称1","一级指标名称2","一级指标名称3"};
                 //下拉选在Excel中对应的列
                 mapDropDown.put(0,nameSelect);
                 //获取工作簿
                 Sheet sheet = writeSheetHolder.getSheet();
                 ///开始设置下拉框
                 DataValidationHelper helper = sheet.getDataValidationHelper();
                 //设置下拉框
                 for (Map.Entry<Integer, String[]> entry : mapDropDown.entrySet()) {
                     /*起始行、终止行、起始列、终止列  起始行为1即表示表头不设置**/
                     CellRangeAddressList addressList = new CellRangeAddressList(1, 65535, entry.getKey(), entry.getKey());
                     /*设置下拉框数据**/
                     DataValidationConstraint constraint = helper.createExplicitListConstraint(entry.getValue());
                     DataValidation dataValidation = helper.createValidation(constraint, addressList);
                     dataValidation.setShowErrorBox(true);
                     sheet.addValidationData(dataValidation);
                 }
             }
         };
    }



}
